#include "server.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main (int argc, char* argv[])
{
  int nb_thread = 0;
  char* num_port = NULL;
  char* emptr = NULL;
  char *directory = NULL;
  
  if (argc != 7)
    {
      fprintf(stderr, "Usage %s -t <thread> -p <numero de port> -d <répertoire>\n", argv[0]);
      exit(1);
    }

  if (argv[1][1]=='t')
    {
      nb_thread = strtol(argv[2],&emptr,10);
      
      if(emptr == argv[1])
        {
	  perror("Erreur de conversion");
	  fprintf(stderr,"Usage %s -t <thread> -p <numero de port> -d <répertoire>\n", argv[0]);
	  exit(1);
        }
    }
  else if(argv[1][1] == 'p')
    {
      num_port = argv[2];
    }
  else if(argv[1][1] == 'd')
    {
      directory = argv[2];
    }

  if (argv[3][1]=='t')
    {
      nb_thread = strtol(argv[4],&emptr,10);
      
      if(emptr == argv[4])
        {
	  perror("Erreur de conversion");
	  fprintf(stderr,"Usage %s -t <thread> -p <numero de port> -d <répertoire>\n", argv[0]);
	  exit(1);
        }
    }
  else if(argv[3][1] == 'p')
    {
      num_port = argv[4];
    }
  else if(argv[3][1] == 'd')
    {
      directory = argv[4];
    }

  if (argv[5][1]=='t')
    {
      nb_thread = strtol(argv[6],&emptr,10);
      
      if(emptr == argv[6])
        {
	  perror("Erreur de conversion");
	  fprintf(stderr,"Usage %s -t <thread> -p <numero de port> -d <répertoire>\n", argv[0]);
	  exit(1);
        }
    }
  else if(argv[5][1] == 'p')
    {
      num_port = argv[6];
    }
  else if(argv[5][1] == 'd')
    {
      directory = argv[6];
    }

  printf("Démarrage du serveur sur le port %s avec %d Threads de services\n", num_port, nb_thread);
  server(nb_thread, num_port, directory, nb_thread*2);
  return 0;
}
